import java.util.Stack;

public class MinStack {
    //最小栈
    //https://leetcode.cn/problems/min-stack/
    public Stack<Integer> stack;
    public Stack<Integer> minstack;

    public MinStack() {
        //初始化
        stack = new Stack<>();
        minstack = new Stack<>();
    }

    public void push(int val) {
        //入栈
        stack.push(val);

        if (minstack.empty()) {
            minstack.push(val);
        }else {
            if (val <= minstack.peek()) {
                minstack.push(val);
            }
        }
    }

    public void pop() {
        //出栈
        if (stack.empty()) {
            return;
        }

        int x = stack.pop();

        if (x == minstack.peek()) {
            minstack.pop();
        }
    }

    public int top() {
        //返回栈顶数据
        if (stack.empty()) {
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        //返回栈中的最小数据
        if (minstack.empty()) {
            return -1;
        }

        return minstack.peek();
    }
}
